﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Product>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@functions {
    private string SortOrderLink(string displayName, string name, string search, int pageSize, bool isTrash, int? categoryId)
    {
        string url = Url.Action("Index", new
        {
            sortOrder = ViewData[name + "Sort"],
            search = search,
            pageSize = pageSize,
            isTrash = isTrash
        });

        string icon = String.IsNullOrWhiteSpace(ViewData[name + "Icon"] as String)
            ? DataTableConstant.DefaultSortIcon
            : ViewData[name + "Icon"] as String;

        return "<a href='" + url + "'>" + displayName + icon + "</a>";
    }
}

@{
    var strHp = new StringHelper();
    var imgHp = new ImageHelper();
    
    string title = ViewBag.Title;
    string search = ViewBag.Search;
    string sortOrder = ViewBag.SortOrder;
    int pageSize = ViewBag.PageSize;
    bool isTrash = ViewBag.IsTrash;
    int? categoryId = ViewBag.CategoryId;
    int trashCount = ViewBag.TrashCount;
    int listCount = ViewBag.ListCount;
    string returnListUrl = ViewBag.ReturnListUrl;
    SelectList pageSizeDDL = ViewBag.PageSizeDDL;
    SelectList categoryDDL = ViewBag.CategoryDDL;

    int index = Model.FirstItemOnPage;
    string controller = "product";

    ViewBag.Title = "Danh sách " + title;
}

<section class="content-header">
    <h1>
        @title.ToUpper() <small>@(!isTrash ? "danh sách" : "thùng rác")</small>
    </h1>
    <ol class="breadcrumb">
        <li>
            <a href="@Url.Action("Index", "Admin")">
                <i class="fa fa-home"></i> Trang chủ
            </a>
        </li>
        <li class="active">@(!isTrash ? "Danh sách" : "Thùng rác") @title</li>
    </ol>
</section>

<section class="content">
    @if (isTrash)
    {
        <div class="alert alert-warning alert-dismissable">
            <i class="fa fa-warning"></i>
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
            <b>Lưu ý: </b> Khi bạn đồng ý "xóa vĩnh viễn" một sản phẩm,
            đồng nghĩa với việc bạn "xóa vĩnh viễn" các lịch sử mua sản phẩm đó.
        </div>
    }

    @Html.Action("Alert", "Layout", new { partialView = "_AlertHandling" })

    <div class="box box-primary">
        <div class="box-body">
            @using (Html.BeginForm("Index", "Product", FormMethod.Get))
            {
                @Html.Hidden("IsTrash", isTrash)

                <div class="row">
                    <div class="col-md-7">
                        @Html.DropDownList("PageSize", pageSizeDDL, new
                        {
                            @class = "form-control auto-width inline",
                            onchange = "this.form.submit()"
                        })

                        @Html.DropDownList("CategoryId", categoryDDL, "Tất cả danh mục", new
                        {
                            @class = "form-control auto-width inline",
                            onchange = "this.form.submit()"
                        })

                        @if (!isTrash)
                        {
                            bool roleCreate = User.IsInRole("Admin") || User.IsInRole("Product Create");
                            
                            if (roleCreate)
                            {
                                <a href="@Url.Action("Create", new { returnListUrl = returnListUrl })" class="btn btn-warning">
                                    <i class="fa fa-plus"></i> Tạo mới
                                </a>
                            }
                            <a href="@Url.Action("Index", new { isTrash = true })" class="btn btn-danger @(trashCount > 0 ? "" : "disabled")">
                                <i class="fa fa-trash-o"></i> Thùng rác (@trashCount)
                            </a>
                        }
                        else
                        {
                            <a href="@Url.Action("Index")" class="btn btn-success">
                                <i class="fa fa-file-text"></i> Danh sách (@listCount)
                            </a>
                        }
                    </div>
                    <div class="col-md-5">
                        <div class="input-group">
                            @Html.TextBox("Search", search, new
                            {
                                @class = "form-control",
                                name = "search",
                                placeholder = "Tên / Mã " + title
                            })
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="submit">
                                    <i class="fa fa-search"></i> Tìm kiếm
                                </button>
                            </span>
                        </div>
                    </div>
                </div>
            }
            <br />
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th></th>
                            <th>
                                @Html.Raw(SortOrderLink("Tên", "Name", search, pageSize, isTrash, categoryId))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Đơn giá", "ListPrice", search, pageSize, isTrash, categoryId))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Danh mục", "Category.Name", search, pageSize, isTrash, categoryId))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Tạo mới", "DateCreated", search, pageSize, isTrash, categoryId))
                            </th>
                            <th>
                                @Html.Raw(SortOrderLink("Chỉnh sửa", "DateModified", search, pageSize, isTrash, categoryId))
                            </th>
                            <th class="text-center">
                                <a id="btnRefresh" href="@Request.Url.AbsoluteUri" class="btn btn-success btn-sm hidden">
                                    <i class="fa fa-refresh"></i> Làm mới
                                </a>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (Product item in Model)
                        {
                            bool roleStyle = User.IsInRole("Admin") || User.IsInRole("Style Read Public")
                                || (User.IsInRole("Style Read Private") && item.CreatedBy == User.Identity.Name);

                            bool roleImage = User.IsInRole("Admin") || User.IsInRole("Image Read Public")
                                || (User.IsInRole("Image Read Private") && item.CreatedBy == User.Identity.Name);

                            bool roleVideo = User.IsInRole("Admin") || User.IsInRole("Video Read Public")
                                || (User.IsInRole("Video Read Private") && item.CreatedBy == User.Identity.Name);
                            
                            bool roleEdit = User.IsInRole("Admin") || User.IsInRole("Product Edit Public")
                                || (User.IsInRole("Product Edit Private") && item.CreatedBy == User.Identity.Name);

                            bool roleRecycle = User.IsInRole("Admin") || User.IsInRole("Product Recycle Public")
                                || (User.IsInRole("Product Recycle Private") && item.CreatedBy == User.Identity.Name);

                            bool roleDelete = User.IsInRole("Admin") || User.IsInRole("Product Delete Public")
                                || (User.IsInRole("Product Delete Private") && item.CreatedBy == User.Identity.Name);                            
                            
                            // Image
                            Image image = item.Images.Where(x => !x.IsDelete)
                                .OrderByDescending(x => x.Priority)
                                .FirstOrDefault();
                            string thumbUrl = "/img/no-image.png?w=50&h=50";
                            bool isStandard = false;
                            if (image != null)
                            {
                                if (imgHp.Standard(image.Width, image.Height))
                                {
                                    thumbUrl = image.Url + "?w=50";
                                    isStandard = true;
                                }
                                else
                                {
                                    thumbUrl = image.Url + "?w=50&h=50";
                                }
                            }

                            // Style
                            float minPrice = 0;
                            float maxPrice = 0;
                            if (item.Styles.Count > 0)
                            {
                                minPrice = item.Styles.Min(x => x.UnitPrice);
                                maxPrice = item.Styles.Max(x => x.UnitPrice);
                            }

                            <tr>
                                <td>
                                    @(index++)
                                </td>
                                <td class="text-center no-padding">
                                    <img class="center-block @(isStandard == true ? "" : "no-standard")" src="@thumbUrl" />
                                </td>
                                <td>
                                    <a class="no-href" data-id="@item.Id" data-toggle="modal" data-target="#detailsModal"
                                       title="Chi tiết" onclick="Details(this, '@(controller)')">
                                        <div>@item.Name</div>
                                        <div>@item.Code</div>
                                    </a>
                                </td>
                                <td class="text-right">
                                    <div class="text-danger">
                                        @strHp.PriceFormat(item.ListPrice, "usd")
                                    </div>
                                    <div class="text-green">
                                        @if (minPrice == maxPrice)
                                        {
                                            <span>@strHp.PriceFormat(minPrice, "usd")</span>
                                        }
                                        else
                                        {
                                            <span>@strHp.PriceFormat(minPrice, "usd") - @strHp.PriceFormat(maxPrice, "usd")</span>
                                        }
                                    </div>
                                </td>
                                <td>
                                    <div>
                                        @item.Category.Name
                                    </div>
                                    <div>
                                        Cấp @item.Category.Level
                                    </div>
                                </td>
                                <td>
                                    <div>
                                        @item.DateCreated.ToString(DataTableConstant.DateTimeFormat)
                                    </div>
                                    <div>
                                        @item.CreatedBy
                                    </div>
                                </td>
                                <td>
                                    @if (item.DateModified != null)
                                    {
                                        <div>
                                            @(((DateTime)item.DateModified).ToString(DataTableConstant.DateTimeFormat))
                                        </div>
                                        <div>
                                            @item.ModifiedBy
                                        </div>
                                    }
                                </td>
                                <td class="control">
                                    @if (!isTrash)
                                    {
                                        if (roleStyle)
                                        {
                                            <a href="@Url.Action("Index", "Style", new { productId = item.Id })"
                                               data-toggle="tooltip" data-placement="top"
                                               title="Giá (@item.Styles.Count)">
                                                <i class="fa fa-usd @(item.Styles.Count > 0 ? "" : "text-danger")"></i>
                                            </a>
                                        }
                                        if (roleImage)
                                        {
                                            <a href="@Url.Action("Index", "Image", new { productId = item.Id })"
                                               data-toggle="tooltip" data-placement="top"
                                               title="Ảnh (@item.Images.Count)">
                                                <i class="fa fa-picture-o @(item.Images.Count > 0 ? "" : "text-danger")"></i>
                                            </a>
                                        }
                                        if (roleVideo)
                                        {
                                            <a href="@Url.Action("Index", "Video", new { productId = item.Id })"
                                               data-toggle="tooltip" data-placement="top"
                                               title="Video (@item.Videos.Count)">
                                                <i class="fa fa-video-camera @(item.Videos.Count > 0 ? "" : "text-danger")"></i>
                                            </a>
                                        }
                                        if (roleEdit)
                                        {
                                            <a href="@Url.Action("Edit", new { id = item.Id, returnListUrl = returnListUrl })"
                                               data-toggle="tooltip" data-placement="top" title="Chỉnh sửa">
                                                <i class="fa fa-edit"></i>
                                            </a>
                                        }
                                        if (roleRecycle)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                               title="Xóa tạm thời" onclick="Recycle(this, '@controller', 0)">
                                                <i class="fa fa-trash-o"></i>
                                            </a>
                                        }
                                    }
                                    else
                                    {
                                        if (roleRecycle)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="tooltip" data-placement="top"
                                               title="Khôi phục" onclick="Recycle(this, '@controller', 1)">
                                                <i class="fa fa-repeat"></i>
                                            </a>
                                        }
                                        if (roleDelete)
                                        {
                                            <a class="no-href" data-id="@item.Id" data-toggle="modal" data-target="#deleteModal"
                                               title="Xóa vĩnh viễn" onclick="Details(this, '@controller')">
                                                <i class="fa fa-times"></i>
                                            </a>
                                        }
                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
            <br />
            <div class="row">
                <div class="col-md-6">
                    <div class="well-sm">
                        Hiển thị <span class="badge">@Model.FirstItemOnPage</span>
                        đến <span class="badge">@Model.LastItemOnPage</span>
                        trong <span class="badge">@Model.TotalItemCount</span> @title
                    </div>
                </div>
                <div class="col-md-6 text-right">
                    @Html.PagedListPager(Model, page => Url.Action("Index", new
                    {
                        page,
                        sortOrder = sortOrder,
                        search = search,
                        pageSize = pageSize,
                        isTrash = isTrash
                    }), new PagedListRenderOptions
                    {
                        LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                        LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                        LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                        LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                        MaximumPageNumbersToDisplay = 5
                    })
                </div>
            </div>
        </div>
    </div>
</section>

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DetailsModal",
    title = title
})

@Html.Action("ManagerModal", "Layout", new
{
    partialView = "_DeleteModal",
    title = title,
    controller = controller,
    returnListUrl = returnListUrl
})

@section scripts{
    <script>
        ActiveMenu('@controller');
    </script>
}